package com.crm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.annotation.Log;
import com.crm.base.BusinessType;
import com.crm.base.OperatorType;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.*;
import com.crm.domain.entity.Business;
import com.crm.domain.entity.BusinessPool;
import com.crm.domain.entity.BusinessRecord;
import com.crm.domain.query.BusinessQuery;
import com.crm.mapper.BusinessMapper;
import com.crm.service.BusinessService;
import com.crm.service.IBusinessPoolService;
import com.crm.service.IBusinessRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Hulunico,Xanny
 */
@RestController
@Api(tags = "商机管理")
@RequestMapping("/business")
@RequiredArgsConstructor
public class BusinessController {
    private final BusinessService businessService;

    private final BusinessMapper businessMapper;

    /**
     * 分页查询商机改
     * @param queryBusiness
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取商机列表")
    @Log(title = "操作日志",businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
    public ResponseResult getByPage(QueryBusiness queryBusiness){
          Page<Business> pageInfo =businessService.getByPage(queryBusiness);
           return ResponseResult.success()
                   .put("total",pageInfo.getTotal())
                   .put("rows",pageInfo.getRecords());
    }



    /**
     * 新增
     * @param businessDto
     * @return
     */
    @ApiOperation("新增商机")
    @PostMapping
    @Log(title = "操作日志",businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
    public ResponseResult addBusiness(@RequestBody BusinessDto businessDto){
        Business business = BeanUtil.copyProperties(businessDto, Business.class);
        businessService.save(business);
       return ResponseResult.success();
    }


    private final IBusinessRecordService businessRecordService;
    /**
     * 保存
     * 需要完成两部分数据的修改 1.修改基本信息 2.新增record
     * @param dto
     * @return
     */
    @PostMapping("/record")
    @Log(title = "操作日志",businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
    public ResponseResult updateBusiness(@RequestBody BusinessRecordDto dto){
        Business business = BeanUtil.copyProperties(dto, Business.class);
        //完成基本信息的修
        businessService.lambdaUpdate().eq(Business::getPhone,dto.getPhone()).update(business);

        //完成record的新增
        businessRecordService.add(dto);
        return ResponseResult.success();
    }


    /**
     * 查询BusinessRecord
     * @param businessId
     * @return
     */
    @GetMapping("/record/list")
    @ApiOperation("根据商机id查询沟通记录")
    public ResponseResult selectById(Integer businessId){
        List<BusinessRecord> list = businessRecordService.lambdaQuery().eq(BusinessRecord::getBusinessId, businessId).list();
        return ResponseResult.success(list);
    }

    @PutMapping("/back/{id}/{reasonId}")
    @ApiOperation("踢回公海")
    @Log(title = "操作日志",businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
    public ResponseResult kickBack(
            @PathVariable("id") Long id,
            @PathVariable("reasonId") Long reasonId) {
        return businessService.kickBack(id, reasonId);
    }


    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商机")
    public ResponseResult selectId(@PathVariable Long id){
        Business business = businessService.getById(id);
        return ResponseResult.success(business);
    }


    private final IBusinessPoolService businessPoolService;
    @GetMapping("/pool")
    @ApiOperation("公海池列表")
    public ResponseResult list(BusinessQuery query) {
         Page<BusinessPool> pageInfo=businessPoolService.getList(query);
         return ResponseResult.success()
                 .put("total",pageInfo.getTotal())
                 .put("rows",pageInfo.getRecords());
     }


    @PutMapping("/gain")
    @ApiOperation("捞取公海池")
    public ResponseResult gain(@RequestBody AssignmentDto dto) {
        return businessPoolService.gain(dto);
    }

    @PutMapping("/assignment")
   public ResponseResult assignment(@RequestBody GiveBusinessDto dto){
        return businessService.assignment(dto);
   }

    }




